package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.po.PoctProject;
import com.rzdata.poct.system.domain.business.bo.PoctProjectBo;
import com.rzdata.poct.system.domain.business.vo.PoctProjectVo;
import com.rzdata.poct.system.mapper.PoctProjectMapper;
import com.rzdata.poct.system.service.IPoctProjectService;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * poct 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctProjectServiceImpl extends ServicePlusImpl<PoctProjectMapper, PoctProject, PoctProjectVo> implements IPoctProjectService {

    @Override
    public PoctProjectVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctProjectVo> queryPageList(PoctProjectBo bo) {
        PagePlus<PoctProject, PoctProjectVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctProjectVo> queryList(PoctProjectBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctProject> buildQueryWrapper(PoctProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctProject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), PoctProject::getProjectNo, bo.getProjectName())
            .or()
            .like(StringUtils.isNotBlank(bo.getProjectName()), PoctProject::getProjectName, bo.getProjectName());
        lqw.eq(bo.getInspectType() != null, PoctProject::getInspectType, bo.getInspectType());
        lqw.eq(bo.getDeptId() != null, PoctProject::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), PoctProject::getDeptName, bo.getDeptName());
        lqw.like(StringUtils.isNotBlank(bo.getProjectManager()), PoctProject::getProjectManager, bo.getProjectManager());
        lqw.eq(bo.getInstrumentType() != null, PoctProject::getInstrumentType, bo.getInstrumentType());
        lqw.eq(bo.getStatus() != null, PoctProject::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), PoctProject::getDescription, bo.getDescription());
        lqw.eq(bo.getPublishTime() != null, PoctProject::getPublishTime, bo.getPublishTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctProjectBo bo) {
        PoctProject add = BeanUtil.toBean(bo, PoctProject.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctProjectBo bo) {
        PoctProject update = BeanUtil.toBean(bo, PoctProject.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
